#include "companylistwidget.h"
#include "ui_companylistwidget.h"

CompanyListWidget::CompanyListWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CompanyListWidget)
{
    ui->setupUi(this);
    init();
}

CompanyListWidget::~CompanyListWidget()
{
    delete ui;
}

void CompanyListWidget::clear()
{
    setCardList(QList<model::CompanyInfo>());
}

#include <QScrollBar>
#include "companycard.h"
void CompanyListWidget::setCardList(const QList<model::CompanyInfo> &companys)
{
    if(!_scrollArea || !_container || !_container->layout()) return;
    this->hide();

    QLayoutItem *item;
    while ((item = _container->layout()->takeAt(0)) != nullptr) {
        if (item->widget())
            delete item->widget();
        delete item;
    }

    auto setLayout = [](QWidget* container) {
        QHBoxLayout *layout = new QHBoxLayout(container);
        layout->setContentsMargins(0, 0, 0, 0);
        layout->setSpacing(0);
        return layout;
    };

    auto getContainer = [this, setLayout]{
        QWidget *container = new QWidget(this);
        container->setFixedHeight(200);
        container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        auto layout = setLayout(container);

        for (int i = 0; i < 3; ++i) {
            QWidget *card = new QWidget(container);
            card->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            layout->addWidget(card);
            layout->setStretch(i, 1);
        }
        return container;
    };

    QWidget *cur = nullptr;
    for(int i = 0, size = companys.size(); i <= size; ++i) {
        if (i == size) {
            if (cur)
                _container->layout()->addWidget(cur);
            break;
        }
        if (i % 3 == 0) {
            if (cur)
                _container->layout()->addWidget(cur);
            cur = getContainer();
        }
        QHBoxLayout* curlayout = (QHBoxLayout*)cur->layout();
        auto layout = setLayout(curlayout->itemAt(i % 3)->widget());
        auto card = new CompanyCard(_container);
        layout->addWidget(card);
        auto companyInfo = companys[i];
        card->set(companyInfo);
        connect(card, &CompanyCard::clicked, [this, companyInfo]{
            this->showCompany(companyInfo);
        });
    }

    _scrollArea->verticalScrollBar()->setValue(0);
    this->show();
}

void CompanyListWidget::init()
{
    initPanel();
    initBody();
    initStyleSheet();
}

#include "pagecontroller.h"
void CompanyListWidget::initStyleSheet()
{
    ui->panel->setStyleSheet(R"(background-color: transparent;)");
    ui->head->setStyleSheet(R"(background-color: transparent;)");
    ui->body->setStyleSheet(R"(background-color: transparent;)");
    ui->lineEdit_search->setStyleSheet(R"(QLineEdit { padding-left: 30px; border-radius: 15px; border: 1px solid transparent; background: qlineargradient( x1:0, y1:0, x2:1, y2:0, stop:0 #dbeafe, stop:1 #fce7f3 ); color: #4b5563; font-size: 22px; background-repeat: no-repeat; background-position: 10px center; background-origin: content; background-clip: padding; } QLineEdit:!focus { color: #9ca3af; })");

    ui->btn_recommend->setStyles(R"(border-radius: 12px; color: #787F8D;)", R"(background-color: transparent; )", R"(background-color: #F9CFE4; )");
    ui->btn_latest->setStyles(R"(border-radius: 12px; color: #787F8D;)", R"(background-color: transparent; )", R"(background-color: #F9CFE4; )");
    auto controller = new PageController(nullptr, this);
    controller->addPage(ui->btn_recommend, nullptr);
    controller->switchToPage(controller->addPage(ui->btn_latest, nullptr));
}

void CompanyListWidget::initPanel()
{
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->layout()->setContentsMargins(20, 0, 0, 0);
    this->layout()->setSpacing(0);

    ui->panel->layout()->setContentsMargins(0, 0, 0, 0);
    ui->panel->layout()->setSpacing(0);

    ui->head->setFixedHeight(60);
    ui->head->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    ui->head->layout()->setContentsMargins(0, 5, 10, 5);
    ui->head->layout()->setSpacing(10);

    ui->lineEdit_search->setFixedSize(350, 50);
    ui->btn_recommend->setFixedSize(60, 40);
    ui->btn_latest->setFixedSize(60, 40);

    ui->body->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

#include <QScrollArea>
void CompanyListWidget::initBody()
{
    // 1) 新建一个主布局，设置给 ui->body
    QVBoxLayout *mainLayout = new QVBoxLayout(ui->body);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // 2) 新建 QScrollArea 及其内部容器
    _scrollArea = new QScrollArea(ui->body);

    _container = new QWidget;
    _container->setStyleSheet(R"( background-color: transparent;)");

    QVBoxLayout* containerLayout = new QVBoxLayout(_container);
    containerLayout->setSpacing(10);
    containerLayout->setContentsMargins(20, 0, 20, 0);
    containerLayout->setAlignment(Qt::AlignTop); // 布局内所有子控件顶对齐

    // 3) 设置 scrollArea
    _scrollArea->setWidget(_container);
    _scrollArea->setWidgetResizable(true);
    _scrollArea->setStyleSheet(R"( QScrollArea { border: none; background-color: transparent; } QScrollArea > QWidget { background-color: transparent; } QScrollBar:vertical { background-color: transparent; /* 透明背景 */ width: 9px; border: none; } QScrollBar::handle:vertical { background-color: #dcdfe4; border-radius: 4px; min-height: 20px; /* 最小高度 */ } QScrollBar::handle:vertical:hover { background-color: #bec3ca; } QScrollBar::handle:vertical:pressed { background-color: #a8adb5; /* 新增:按下状态 */ } /* 关键:定义滑块上下的空白区域 */ QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; } QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0px; /* 隐藏箭头 */ background: transparent; } QScrollBar::up-arrow:vertical, QScrollBar::down-arrow:vertical { background: none; } )");

    // 4) 将 scrollArea 加到主布局里
    mainLayout->addWidget(_scrollArea);
}

#include "companydialog.h"
void CompanyListWidget::showCompany(const model::CompanyInfo &companyInfo)
{
    CompanyDialog dialog(this);
    dialog.set(companyInfo);
    dialog.exec();
}

#include "debug.h"
#include "model/datacenter.h"
void CompanyListWidget::on_btn_recommend_clicked()
{
#if TEST_UI
    model::CompanyInfo info;
    info.companyName = "公司名";
    info.windowCnt = 12;
    info.timestamp = time(0);
    info.ci = 12;

    info.topTags.append("tag1");
    info.topTags.append("tag2");
    info.topTags.append("tag3afffdsgqregadgargasgargavzdweqfefasdfwae");
    info.topTagsPct.append(96);
    info.topTagsPct.append(66);
    info.topTagsPct.append(26);
    info.topTags.append("tag1");
    info.topTags.append("tag2");
    info.topTags.append("tag3");
    info.topTagsPct.append(96);
    info.topTagsPct.append(66);
    info.topTagsPct.append(26);
    info.upTags.append("tag1");
    info.upTags.append("tag2");
    info.upTags.append("tag3");
    info.upTagsPct.append(96);
    info.upTagsPct.append(66);
    info.upTagsPct.append(26);
    info.upTags.append("tag1");
    info.upTags.append("tag2");
    info.upTags.append("tag3");
    info.upTagsPct.append(96);
    info.upTagsPct.append(66);
    info.upTagsPct.append(26);
    QList<model::CompanyInfo> list;
    for(int i = 0, n = rand() % 100; i < n; ++i) list.append(info);
    setCardList(list);
#endif
    model::SearchLimits limits;
    limits.searchKey = "推荐";
    auto& dataCenter = model::DataCenter::getInstance();
    dataCenter.searchCompanyAsync(limits);
    connect(&dataCenter, &model::DataCenter::searchCompanyDone, this, &CompanyListWidget::searchCompanyDone, Qt::ConnectionType::UniqueConnection);
}

void CompanyListWidget::searchCompanyDone()
{
    auto ret = model::DataCenter::getInstance().getSearchCompanyResult();
    if (ret)
        setCardList(*ret);
}
